home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / tex / lgrind.zoo / lgrind.c < prev    next >
C/C++ Source or Header  |  1992-09-28  |  41KB  |  1,464 lines

  1. /* lgrind.c, a %%\LaTeX%% version of tgrind, which is a %%\TeX%% version
  2.  * of vgrind.  Used to `grind nice program listings'.
  3.  */
  4.  
  5. #ifndef lint
  6. static char sccsid[] = "@(#)@(#)tfontedpr.c    1.3 (LBL) 4/12/85";
  7. static char Version[] =
  8.    "$Id: lgrind.c,v 1.4 91/10/01 00:36:11 gvr Exp $";
  9. #endif
  10.    
  11. /* lgrind --- general purpose "pretty printer" for use with %%\LaTeX%%.
  12.  *
  13.  * Copyright %%\copyright%% 1985 by Van Jacobson, Lawrence Berkeley Laboratory
  14.  * This program may be freely used and copied but may not be sold
  15.  * without the author's written permission.  This notice must remain
  16.  * in any copy or derivative.
  17.  *
  18.  * This program is an adaptation of "vfontedpr" v4.2 (12/11/84) from
  19.  * the 4.2bsd Unix distribution.  Vfontedpr was written by Dave
  20.  * Presotto (based on an earlier program of the same name written by
  21.  * Bill Joy).
  22.  *
  23.  * I would welcome comments, enhancements, bug fixes, etc.  Please 
  24.  * mail them to:
  25.  *    van@@lbl-rtsg.arpa    (from arpanet, milnet, csnet, etc.)
  26.  *    ..!ucbvax!lbl-csam!van    (from Usenet/UUCP)
  27.  *
  28.  * Modifications.
  29.  * --------------
  30.  * 30 Mar 85    Chris & Van    Fixed %|\C|% & %|\S|% (comment & string start
  31.  *                indicators to really appear at the start of
  32.  *                comments & strings.  Changes for speeded-up
  33.  *                @expmatch()@.
  34.  * 29 Mar 85    Chris Torek    (chris@@maryland):  Bug fixes for %|~|% and
  35.  *                %|^L|% output.  Most cpu-time eaters recoded
  36.  *                to improve efficiency.
  37.  * 10 Feb 85    Van        Written.
  38.  *  8 Oct 87    JSL        Modified so as to compile on VMS.  On VMS, we
  39.  *                don't use a surrounding shell script, so the
  40.  *                options are a bit different --- added %|-i|% to
  41.  *                make a file to be %|\input|% in a %%\LaTeX%%
  42.  *                environment.  Changed some of the characters
  43.  *                around --- e.g., we always take %|*|% in math
  44.  *                mode (to make it centered) so we have to
  45.  *                write %|*|% as %|\*|%.  Etc.
  46.  * 14 Jan 88    JSL        %|-e|% option for embedded code.
  47.  * 22 Jan 88    JSL        Bugfix --- program could overrun @pstack@
  48.  *                because of an incorrect limit test.
  49.  *    Sep 91    George V Reilly    Reformated and cleaned up code, including
  50.  *                naughtiness with @NULL@.  Added %|@|%,
  51.  *                %|%%|%, %|%$|%, and %%\tt \%|%% features.
  52.  *                Also the %|%<|%, %|%!|%, and %|%#|% features.
  53.  */
  54.    
  55. #include <stdio.h>
  56. #include <ctype.h>
  57.  
  58. #ifndef vms
  59. #  include <sys/types.h>
  60. #  include <sys/stat.h>
  61. #  include <time.h>
  62. #else
  63. #  include <types.h>
  64. #  include <stat.h>
  65. #endif
  66.    
  67. typedef    int boolean;
  68.  
  69. #define TRUE  1
  70. #define FALSE 0
  71.  
  72. #define STANDARD 0
  73. #define ALTERNATE 1
  74.    
  75. #define    NOTCODE 0        /* the three states of @incode@ */
  76. #define INITCODE 1
  77. #define OTHERCODE 2
  78.  
  79. #define PNAMELEN 80        /* length of a function/procedure name */
  80. #define PSMAX 20        /* size of procedure name stacking */
  81.    
  82. #define    BUFFERSIZE    4096
  83.  
  84. #ifndef vms
  85. #  define OKEXIT    0
  86. #  define BADEXIT    1
  87. #  ifndef DEFSFILE
  88. #    define DEFSFILE    "/usr/lib/lgrindefs"
  89. #  endif
  90. #else
  91. #  define OKEXIT    1
  92. #  define BADEXIT    0
  93. #  define DEFSFILE    "TEX$INPUTS:lgrindefs.src"
  94. #endif
  95.    
  96.  
  97. /* regular expression routines */
  98.    
  99. char    *expmatch();        /* match a string to an expression */
  100. char    *convexp();        /* convert expression to internal form */
  101.  
  102.  
  103. /* database query routines */
  104.  
  105. char    *tgetstr();
  106. int     tgetent();
  107. int     tgetflag();
  108.  
  109.  
  110. /* forward declarations */
  111.  
  112. void    setlang();
  113. void    readfile();
  114. void    putScp();
  115. void    putKcp();
  116. void    putVcp();
  117. void    putstr();
  118. boolean    isproc();
  119. void    outchar();
  120.  
  121.  
  122. /*
  123.  *    The state variables
  124.  */
  125.  
  126. boolean    incomm;            /* in a comment of the primary type */
  127. boolean    instr;            /* in a string constant */
  128. boolean    inchr;            /* in a character constant */
  129. int    incode;            /* in program text within a comment */
  130. int    latexcode;        /* in program text within %%\LaTeX%% */
  131. int    latex_tt;        /* in %|\tt|% text within %%\LaTeX%% */
  132. boolean    use_tt;            /* use %|\tt|% everywhere */
  133. boolean    do_at;            /* pay attention to %|@|%s in %%\LaTeX%% */
  134. boolean    do_tt;            /* pay attention to %|||%s in %%\LaTeX%% */
  135. boolean    nokeyw = FALSE;        /* no keywords being flagged */
  136. boolean    prccont;        /* continue last procedure */
  137. boolean    code_cmnts = TRUE;    /* Treat %|@|%, etc specially in comments */
  138. boolean    code_latex = TRUE;    /* Treat %|@|%, etc specially in %%\LaTeX%% */
  139. boolean    linehead;        /* at beginning of line or after whitespace */
  140. int    lastout;        /* (extended) last character to outchar */
  141. int    comtype;        /* type of comment */
  142. int    psptr;            /* the stack index of the current procedure */
  143. char    pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
  144. int    plstack[PSMAX];        /* the procedure nesting level stack */
  145. int    blklevel;        /* current nesting level */
  146. char    *fname = "";        /* File being read */
  147. int    lineno;            /* Line number in that file */
  148. char    pname[BUFFERSIZE+1];    /* Current procedure name */
  149.  
  150.  
  151. /*
  152.  *    The language specific globals
  153.  */
  154.  
  155. char    *defsfile = DEFSFILE;    /* name of language definitions file */
  156. char    language[PNAMELEN]="c";    /* the language indicator */
  157. char    the_buf[BUFFERSIZE+1];    /* general purpose buffer */
  158. char    *buf = the_buf + 1;    /* @buf-1@ must be valid */
  159. char    strings[2*BUFFERSIZE];    /* store the keywords */
  160. char    defs[2 * BUFFERSIZE];    /* language definitions from lgrindefs */
  161. char    *l_keywds[BUFFERSIZE/2];/* keyword table address */
  162. char    *l_prcbeg;        /* regular expr for procedure begin */
  163. char    *l_combeg;        /* regexp introducing a comment */
  164. char    *l_comend;        /* regexp ending a comment */
  165. char    *l_acmbeg;        /* regexp introducing a comment */
  166. char    *l_acmend;        /* regexp ending a comment */
  167. char    *l_blkbeg;        /* regexp beginning of a block */
  168. char    *l_blkend;        /* regexp ending a block */
  169. char    *l_strbeg;        /* regexp starting string constant */
  170. char    *l_strend;        /* regexp ending string constant */
  171. char    *l_chrbeg;        /* regexp starting character constant */
  172. char    *l_chrend;        /* regexp ending character constant */
  173. char    *l_cdebeg;        /* regexp starting prog text within comment */
  174. char     s_cdebeg[PNAMELEN+1];    /* actual string corresponding to @l_cdebeg@ */
  175. char    *l_cdeend;        /* regexp ending prog text within comment */
  176. char    *l_texbeg;        /* regexp starting %%\TeX%% text in comment */
  177. char    *l_texend;        /* regexp ending %%\TeX%% text in comment */
  178. char    *l_txmbeg;        /* regexp starting %%\TeX%% math in comment */
  179. char    *l_txmend;        /* regexp ending %%\TeX%% math in comment */
  180. char    *l_tt_beg;        /* regexp starting verbatim text in comment */
  181. char    *l_tt_end;        /* regexp ending typewriter text in comment */
  182. char    *l_at;            /* regexp for %|@|% in %%\LaTeX%% text */
  183. char    *l_tt;            /* regexp for %|||% in %%\LaTeX%% text */
  184. char    *l_pc;            /* regexp for %|%|% in %%\LaTeX%% text */
  185. char    l_escape;        /* character used to escape characters */
  186. boolean    l_toplex;        /* procedures only defined at top lex level */
  187. boolean    l_onecase;        /* upper & lower case equivalent */
  188. boolean    embed = FALSE;        /* -e seen --- do embedded code */
  189. boolean    code = TRUE;        /* Looking at code */
  190.  
  191.  
  192. /*
  193.  *  global variables also used by expmatch
  194.  */
  195.  
  196. extern    boolean    _escaped;    /* if last character was an escape */
  197. extern    char *_start;        /* start of the current string */
  198.  
  199.  
  200. extern    int (*re_strncmp)();    /* function to do string compares */
  201. extern    int strncmp();
  202. extern    int lc_strncmp();
  203.  
  204.  
  205.  
  206.    int
  207. main(argc, argv)
  208.    int    argc;
  209.    char    *argv[];
  210. {
  211.    struct stat    stbuf;
  212.    char        *p;
  213.    boolean    hseen = FALSE;        /* -h seen        */
  214.    char        *hstring;        /* header string to use    */
  215.    boolean    iseen = FALSE;        /* -i seen        */
  216.    int        files_done = 0;
  217.    
  218.    argc = getredirection(argc, argv);
  219.    
  220.    argc--, argv++;
  221.    do {
  222.       register char *cp;
  223.       register int i;
  224.       
  225.       if (argc > 0) {
  226.      if (!strcmp(argv[0], "-h")) {
  227.         hseen = TRUE;
  228.         if (argc == 1) {
  229.            hstring = "";
  230.            argc = 0;
  231.            goto rest;
  232.         }
  233.         hstring = argv[1];
  234.         argc--, argv++;
  235.         argc--, argv++;
  236.         if (argc > 0)
  237.            continue;
  238.         goto rest;
  239.      }
  240.      
  241.      /* take input from the standard place */
  242.      if (!strcmp(argv[0], "-")) {
  243.         argc = 0;
  244.         goto rest;
  245.      }
  246.      
  247.      /* Process embedded text */
  248.      if (!strcmp(argv[0], "-e")) {
  249.         embed = TRUE;
  250.         argc--, argv++;
  251.         continue;
  252.      }
  253.  
  254.      /* format for inclusion */
  255.      if (!strcmp(argv[0], "-i")) {
  256.         iseen = TRUE;
  257.         argc--, argv++;
  258.         continue;
  259.      }
  260.  
  261.      /* indicate no keywords */
  262.      if (!strcmp(argv[0], "-n")) {
  263.         nokeyw++;
  264.         argc--, argv++;
  265.         continue;
  266.      }
  267.      
  268.      /* Don't treat %|@|%, etc. specially in comments */
  269.      if (!st